//package com.xiao.foundation.config;
//
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.JsonInclude;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.DeserializationFeature;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.SerializationFeature;
//import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
//import com.xiao.foundation.constant.RabbitMqConstant;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.core.FanoutExchange;
//import org.springframework.amqp.core.ReturnedMessage;
//import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
//import org.springframework.amqp.support.converter.MessageConverter;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//
//@Slf4j
//@Configuration
//public class RabbitConfig implements RabbitTemplate.ReturnsCallback {
//
//    @Bean
//    public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory, MessageConverter jsonMessageConverter) {
//        // rabbitMq新版本开启 ReturnCallback
//        connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
//        // 确认开启ConfirmCallback回调
//        connectionFactory.setPublisherReturns(true);
//        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
//        rabbitTemplate.setMandatory(true);
//        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> log.info("消息入列成功:correlationData({}),ack({}),cause({})", correlationData, ack, cause));
////        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> log.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}", exchange, routingKey, replyCode, replyText, message));
//        rabbitTemplate.setReturnsCallback(this);
//        rabbitTemplate.setMessageConverter(jsonMessageConverter);
//        return rabbitTemplate;
//    }
//
//    @Bean(name = "jsonMessageConverter")
//    public MessageConverter jsonMessageConverter(JavaTimeModule javaTimeModule) {
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
//        om.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
//        om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//        om.registerModule(javaTimeModule);
//        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter(om);
//        return converter;
//    }
//
//    @Override
//    public void returnedMessage(ReturnedMessage returnedMessage) {
//        log.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}",
//                returnedMessage.getExchange(),
//                returnedMessage.getRoutingKey(),
//                returnedMessage.getReplyCode(),
//                returnedMessage.getReplyText(),
//                returnedMessage.getMessage());
//
//    }
//
//    @Bean
//    public FanoutExchange auditLogTriggered() {
//        return new FanoutExchange(RabbitMqConstant.EXCHANGE_FANOUT_AUDIT_LOG_TRIGGERED, true, false);
//    }
//}
